package com.bos.service.impl;

import com.bos.data.model.BosParamModel;
import com.bos.data.model.BosProjectModel;
import com.bos.data.model.SalesModel;
import com.bos.data.repositories.SalesRepository;
import com.bos.data.repositories.jpa.SalesJPARepository;
import com.bos.mapper.SalesMapper;
import com.bos.service.SalesService;
import com.bos.common.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service("salesServiceImpl")
public class SalesServiceImpl implements SalesService {
    private Logger logger = LoggerFactory.getLogger(SalesServiceImpl.class);
    //将dao层注入
    @Autowired
    SalesMapper salesMapper;
    @Autowired
    private SalesJPARepository salesJPARepository;

    @Autowired
    private SalesRepository salesRepository;

    //查询出sals表中的数据
    @Override
    public PageData findSales(List<String> leaderList, List<String> statusList, int pageSize, int pageIndex) {
        return salesRepository.findSales(leaderList, statusList, pageSize, pageIndex);
    }

    //2.将数据插入到Sales表中
    @Override
    public int insertSales(SalesModel sales) {
        // TODO Auto-generated method stub
        return salesMapper.insertSales(sales);
    }

    //3.根据id删除数据
    @Override
    public int deleteById(int id) {
        // TODO Auto-generated method stub
        return salesMapper.deleteById(id);
    }

    //4.根据id查询出对应的数据
    @Override
    public SalesModel findSalsById(int id) {
        return salesMapper.findSalsById(id);
    }

    @Transactional
    //5.根据id修改Sales中的数据
    @Override
    public int updateByiD(SalesModel sales) {
        return salesMapper.updateByiD(sales);
    }

    @Override
    public List<BosParamModel> findBos_params(String typeName) {
        return salesMapper.findBos_params(typeName);
    }

    @Override
    public List<SalesModel> selectByAll(String[] sleader, String[] status) {
        return salesMapper.selectByAll(sleader, status);
    }

    @Transactional
    @Override
    public void insertNewSales(SalesModel salesModel) {
        try {
            salesJPARepository.save(salesModel);
        } catch (Exception e) {
            logger.error("插入销售表错误：[{}]", salesModel);
        }
    }

    @Transactional
    @Override
    public String copySales(int id){
        try {
            SalesModel sale=salesJPARepository.findOne(id);
            SalesModel newSale=new SalesModel();
            BeanUtils.copyProperties(sale,newSale);
            newSale.setId(null);
            newSale.setSname("待重命名_"+sale.getSname());
            newSale.setUpdate_time(new Timestamp(System.currentTimeMillis()));
            salesJPARepository.save(newSale);
            return sale.getSname();
        }catch (Exception e){
            logger.error("copy销售数据失败:id{}",id,e);
            return null;
        }
    }
}
